/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mscs.client.member.logic;

import java.util.ArrayList;
import java.util.List;
import mscs.data.IFileDataStore;
import mscs.data.IGiftRecordProxy;
import mscs.data.IGoodsProxy;
import mscs.data.IMemberProxy;
import mscs.data.ISaleRecordProxy;
import mscs.data.pojo.GiftRecord;
import mscs.data.pojo.Goods;
import mscs.data.pojo.Member;
import mscs.data.pojo.SaleRecord;

/**
 *
 * @author Administrator
 */
public class GiftServiceUtil implements IGiftServiceUtil{
    private ArrayList<GiftRecord> giftRecords = null;
    private ArrayList<SaleRecord> saleRecords = null;
    private ArrayList<Member> allMemberList = null;
    private ArrayList<Goods> allGiftList = null;
    private IFileDataStore fdsGiftRecord = new IGiftRecordProxy();
    private IFileDataStore fdsSaleRecord = new ISaleRecordProxy();
    private IFileDataStore fdsMember = new IMemberProxy();
    private IFileDataStore fdsGift = new IGoodsProxy();
    
    public GiftServiceUtil(){
        giftRecords = (ArrayList<GiftRecord>)fdsGiftRecord.getObjects();
        saleRecords =(ArrayList<SaleRecord>)fdsSaleRecord.getObjects();
        allMemberList = (ArrayList<Member>)fdsMember.getObjects();
        allGiftList = (ArrayList<Goods>)fdsGift.getObjects();
    }
    
    public boolean isExist(IGiftUtil giftUtil, List<Integer> reasonList) {
        return giftUtil.getGiftMembers(allMemberList, giftRecords, reasonList).isEmpty();
    }

    public List<Member> getGiftMemberList(IGiftUtil giftUtil, List<Integer> reasonList) {
        return giftUtil.getGiftMembers(allMemberList, giftRecords, reasonList);
    }

    public List<SaleRecord> getMemberSaleList(Member member) {
        return fdsSaleRecord.getObjectsByAttr("memberID", member.getId());
    }

    public boolean commitGiftService(GiftRecord giftRecord) {
        this.giftRecords.add(giftRecord);
        fdsGiftRecord.update(this.giftRecords);
        for(Goods gift: this.allGiftList){
            if(gift.getId().equals(giftRecord.getGiftID())){
                gift.setNum(gift.getNum()-giftRecord.getNum());
            }
        }
        this.fdsGift.update(this.allGiftList);
        return true;
    }

    public List<Goods> getAllGifts() {
        return this.allGiftList;
    }

}
